﻿using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Xml.Serialization;

namespace SQLDeploy.Data.Connections
{
    public abstract class _BaseSQLConnection : _BaseConnection
    {
        protected internal SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

        public _BaseSQLConnection() : base() { }

        protected _BaseSQLConnection(string Name)
            : base()
        {
            this.Name = Name;
        }
        
        protected _BaseSQLConnection(string Name, string Data)
            : base(Name, Data) { }

        public override string Data
        {
            get
            {
                return base.Data;
            }
            protected internal set
            {
                base.Data = value;
                builder = new SqlConnectionStringBuilder(base.Data);
            }
        }

        [XmlIgnore]
        public string DataSource
        {
            get { return builder.DataSource; }
            set
            {
                builder.DataSource = value;
                base.Data = builder.ConnectionString;
                OnPropertyChanged("DataSource");
            }
        }

        [XmlIgnore]
        public bool IntegratedSecurity
        {
            get { return builder.IntegratedSecurity; }
            set
            {
                builder.IntegratedSecurity = value;
                base.Data = builder.ConnectionString;
                OnPropertyChanged("IntegratedSecurity");
            }
        }

        [XmlIgnore]
        public string UserId
        {
            get { return builder.UserID; }
            set
            {
                builder.UserID = value;
                Data = builder.ConnectionString;
                OnPropertyChanged("UserId");
            }
        }

        [XmlIgnore]
        public string Password
        {
            get { return builder.Password; }
            set
            {
                builder.Password = value;
                Data = builder.ConnectionString;
                OnPropertyChanged("Password");
            }
        }

        protected internal abstract bool performSQLConnectionTypeValidation(ref SqlConnection connection);

        protected override sealed bool PerformConnectionValidation()
        {
            try
            {
                SqlConnection connection = new SqlConnection(Data);
                int retryCount = 0;
            retry:
                retryCount++;
                try
                {
                    connection.Open();
                    return performSQLConnectionTypeValidation(ref connection);
                }
                catch (SqlException e)
                {
                    if (e.Message.ToLower().Contains("cannot open database") && retryCount <= 2)
                    {
                        //Looks like the DB doesn't exist so remove inital catlog from the con String
                        try{
                            SqlConnectionStringBuilder b = new SqlConnectionStringBuilder(Data);
                            b.InitialCatalog="";
                            connection = new SqlConnection(b.ConnectionString);
                            goto retry;
                        }catch(Exception e1){
                            throw e1;
                        }
                    }
                    throw e;
                }
                catch (Exception e)
                {

                    throw e;
                }
                finally
                {
                    try
                    {
                        if (connection != null)
                            connection.Close();

                    } catch(Exception) {}
                }
            } catch(Exception ex){
                Debug.WriteLine(ex);
                //TODO: Log the Connection Failure?
                return false;
            }
            
        }

    }
}
